<?php

	if ( !isset($_REQUEST["action"]) ) die();
	$action = $_REQUEST["action"];

	include( "bootstrap.php" );

	$data = array();

	// *** Get Projects action
	if ( $action == "getProjects" ) {
		$result = query("SELECT * FROM ".T_PROJECTS);
		$data["projects"] = array();
		if ( mysql_num_rows($result) > 0 ) {
			while ( $obj = mysql_fetch_object($result) ) {
				$data["projects"][] = array( "projectid" => $obj->id, "projectname" => $obj->name, "projectdesc" => $obj->description );
			}
		}
		dieSuccess( "", $data );
	}

	// *** New Project action
	if ( $action == "newProject" ) {
		$name = mysql_real_escape_string($_POST["name"]);
		$desc = mysql_real_escape_string($_POST["desc"]);
		$result = query("INSERT INTO ".T_PROJECTS." (name,description,createdOn) VALUES ('{$name}','{$desc}','".time()."')");
		dieSuccess( "Project successfully created" );
	}

	// *** Delete Project action
	if ( $action == "deleteProject" ) {
		$id = $_POST["projectid"];
		$result = query("DELETE FROM ".T_PROJECTS." WHERE id='{$id}'");
		$result = query("DELETE FROM ".T_NODES." WHERE projectID='{$id}'");
		$result = query("DELETE FROM ".T_COMMENTS." WHERE projectID='{$id}'");
		dieSuccess( "Project successfully deleted" );
	}

?>
